package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SocialUserconnection;
import com.ruoyi.project.system.service.ISocialUserconnectionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 第三方授权Controller
 * 
 * @author ruoyi
 * @date 2020-04-15
 */
@RestController
@RequestMapping("/system/userconnection")
@Api(tags = "第三方授权")
public class SocialUserconnectionController extends BaseController
{
    @Autowired
    private ISocialUserconnectionService socialUserconnectionService;

    /**
     * 查询第三方授权列表
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:list')")
    @GetMapping("/list")
    public TableDataInfo list(SocialUserconnection socialUserconnection)
    {
        startPage();
        Long tenantId = SecurityUtils.getLoginUser().getUser().getTenantId();
        socialUserconnection.setTenantId(tenantId);
        List<SocialUserconnection> list = socialUserconnectionService.selectSocialUserconnectionList(socialUserconnection);
        return getDataTable(list);
    }

    /**
     * 导出第三方授权列表
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:export')")
    @Log(title = "第三方授权", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SocialUserconnection socialUserconnection)
    {
        List<SocialUserconnection> list = socialUserconnectionService.selectSocialUserconnectionList(socialUserconnection);
        ExcelUtil<SocialUserconnection> util = new ExcelUtil<SocialUserconnection>(SocialUserconnection.class);
        return util.exportExcel(list, "userconnection");
    }

    /**
     * 获取第三方授权详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:query')")
    @GetMapping(value = "/{userid}")
    public AjaxResult getInfo(@PathVariable("userid") String userid)
    {
        return AjaxResult.success(socialUserconnectionService.selectSocialUserconnectionById(userid));
    }

    /**
     * 新增第三方授权
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:add')")
    @Log(title = "第三方授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SocialUserconnection socialUserconnection)
    {
        return toAjax(socialUserconnectionService.insertSocialUserconnection(socialUserconnection));
    }

    /**
     * 修改第三方授权
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:edit')")
    @Log(title = "第三方授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SocialUserconnection socialUserconnection)
    {
        return toAjax(socialUserconnectionService.updateSocialUserconnection(socialUserconnection));
    }

    /**
     * 删除第三方授权
     */
    @PreAuthorize("@ss.hasPermi('system:userconnection:remove')")
    @Log(title = "第三方授权", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userid}/{providerid}/{provideruserid}")
    public AjaxResult remove(@PathVariable(value = "userid") String userid, @PathVariable(value = "providerid") String providerid,
                             @PathVariable(value = "provideruserid") String provideruserid)
    {

        return toAjax(socialUserconnectionService.delBindingSocialUser(userid,providerid,provideruserid));
    }
}
